#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/11 0:42


import os
import random
from captcha.image import ImageCaptcha

FONTS_PATH = os.path.join(os.path.dirname(__file__), "../fonts/")


def generateCaptcha(strLen: int = 4, charSet: str = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"):
    fonts = ["font_01.ttf", "font_02.ttf", "font_03.ttf", "font_04.ttf"]
    code = "".join(random.sample(charSet, strLen))
    img = ImageCaptcha(fonts=[os.path.join(FONTS_PATH, file) for file in fonts])
    image = img.generate_image(code)
    return code, image


if __name__ == "__main__":
    import io
    import base64

    code, image = generateCaptcha()
    imgio = io.BytesIO()
    image.save(imgio, "png")
    imgdata = imgio.getvalue()
    print(base64.b64encode(imgdata).decode("utf-8"))
